package com.ruoyi.salesman.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.recycle.domain.MerchantManagement;
import com.ruoyi.recycle.domain.ShopManagement;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.service.IMerchantManagementService;
import com.ruoyi.recycle.service.IShopManagementService;
import com.ruoyi.recycle.service.ISysTypeConfigService;
import com.ruoyi.salesman.dto.ShopManagementDto;
import com.ruoyi.salesman.service.HomeService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


@RestController
@RequestMapping("/home")
public class HomeController {

    @Resource
    private HomeService homeService;
    @Resource
    private IShopManagementService shopManagementService;
    @Resource
    private RemoteUserService userService;
    @Resource
    private ISysTypeConfigService sysTypeConfigService;
    @Resource
    private IMerchantManagementService merchantManagementService;

    /**
     * 平台总数据接口
     */
    @PostMapping("/getConfigType")
    @ApiOperation("获取分类")
    public AjaxResult getConfigType(@RequestParam(value = "configName", required = true) String configName) {

        return AjaxResult.success(sysTypeConfigService.list(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType,"门店分类")));
    }


    /**
     * 平台总数据接口
     */
    @PostMapping("/totalShopData")
    @ApiOperation("平台总数据接口")
    public AjaxResult totalShopData(@RequestParam(value = "salesmanId", required = true) Long salesmanId,
                                    @RequestParam(value = "type", required = true)Integer type,
                                    @RequestParam(value = "signTime", required = false) String signTime,
                                    @RequestParam(value = "shopType", required = false) String shopType) {

        return homeService.totalShopData(salesmanId,type,signTime,shopType);
    }


    /**
     * 单个引流平台详情
     */
    @PostMapping("/oneShopData")
    @ApiOperation("单个引流平台详情")
    public AjaxResult oneShopData(@RequestParam(value = "shopId", required = true) Long shopId,
                                  @RequestParam(value = "type", required = true)Integer type,
                                  @RequestParam(value = "signTime", required = false) String signTime) {

        return homeService.oneShopData(shopId,type,signTime);
    }

    /**
     *引流平台详情
     */
    @PostMapping("/shopDetail")
    @ApiOperation("引流平台详情")
    public AjaxResult shopDetail(@RequestParam("shopId") Long shopId) {

        return AjaxResult.success(shopManagementService.getById(shopId));
    }


    /***
     * 新增/修改引流平台
     */
    @PostMapping("/saveOrUpdateShop")
    @ApiOperation("新增/修改引流平台")
    public AjaxResult saveOrUpdateShop(@RequestBody ShopManagementDto shopManagementDto) {
        ShopManagement shopManagement = new ShopManagement();
        BeanUtils.copyProperties(shopManagementDto,shopManagement);
        if (ObjectUtils.isEmpty(shopManagement.getCreatedTime())) {
            shopManagement.setCreatedTime(new Date());
        }
        shopManagement.setUpdatedTime(new Date());
        shopManagement.setIntegral(0l);
        if (StringUtils.isBlank(shopManagement.getHeadPortrait())){
            shopManagement.setHeadPortrait("https://blue-star-human.oss-cn-guangzhou.aliyuncs.com/20221110/6ac6bb225f0045d8aeda71dea3fbf777.png");
        }

        if (ObjectUtils.isNotEmpty(shopManagement) && shopManagement.getId()!=null){
            ShopManagement managementServiceById = shopManagementService.getById(shopManagement.getId());
            //不允许修改用户名
            if (!managementServiceById.getShopNumber().equals(shopManagement.getShopNumber())){
                return AjaxResult.error("不允许修改门店账户名");

            }
            if (!SecurityUtils.matchesPassword(shopManagementDto.getShopPassword(), managementServiceById.getShopPassword())){
                if (StringUtils.isBlank(shopManagementDto.getOldPassword())){
                    return AjaxResult.error("不允许修改门店密码");
                }
            }
            shopManagementService.updateById(shopManagement);
        }else {

            //新增门店需要同步新增系统user
            SysUser sysUser = new SysUser();
            sysUser.setUserName(shopManagement.getShopNumber());
            sysUser.setPassword(SecurityUtils.encryptPassword(shopManagement.getShopPassword()));
            sysUser.setNickName(shopManagement.getShopName());
            sysUser.setPhonenumber(shopManagement.getPhone());
            sysUser.setStatus("0");
            sysUser.setAvatar(shopManagement.getHeadPortrait());
            R<Boolean> booleanR = userService.registerUserInfo(sysUser, "inner");
            if (booleanR.getCode() == 500){
                return AjaxResult.error("账号已存在，请重新输入账号");
            }
            R<LoginUser> userInfo = userService.getUserInfo(shopManagement.getShopNumber(), "inner");
            SysUser resultUser = userInfo.getData().getSysUser();
            shopManagement.setUserId(resultUser.getUserId());
            shopManagement.setCreatedTime(new Date());
            shopManagement.setShopPassword(SecurityUtils.encryptPassword(shopManagement.getShopPassword()));
            if (ObjectUtils.isNotEmpty(shopManagementDto) && shopManagementDto.getMerchantId() !=null){
                MerchantManagement management = merchantManagementService.getById(shopManagementDto.getMerchantId());
                if (ObjectUtils.isEmpty(management)){
                    return AjaxResult.success("添加门店异常，无法获取当前业务员的综合站信息，请联系管理员处理");
                }
                shopManagement.setMerchantName(management.getMerchantName());
            }

            shopManagementService.save(shopManagement);
        }
        return AjaxResult.success();
    }


    /**
     * 达人总数据
     */
    @PostMapping("/totalClientData")
    @ApiOperation("达人总数据")
    public AjaxResult totalClientData(@RequestParam(value = "salesmanId", required = true) Long salesmanId,
                                      @RequestParam(value = "type", required = true)Integer type,
                                      @RequestParam(value = "signTime", required = false) String signTime) {

        return homeService.totalClientData(salesmanId,type,signTime);
    }


    /**
     * 单个达人数据
     */
    @PostMapping("/oneClientData")
    @ApiOperation("单个达人数据")
    public AjaxResult oneClientData(@RequestParam(value = "clientId", required = true) Long clientId,
                                    @RequestParam(value = "type", required = true)Integer type,
                                    @RequestParam(value = "signTime", required = false) String signTime) {

        return homeService.oneClientData(clientId,type,signTime);
    }



    /**
     * 获取达人信息
     */
    @PostMapping("/clientDetail")
    @ApiOperation("获取达人信息")
    public AjaxResult clientDetail(@RequestParam("clientId") Long clientId) {

        return homeService.clientDetail(clientId);
    }


    /**
     * 我的账单
     */
    @PostMapping("/myBill")
    @ApiOperation("我的账单")
    public AjaxResult myBill(@RequestParam(value = "salesmanId", required = true) Long salesmanId,
                             @RequestParam(value = "type", required = true)Integer type,
                             @RequestParam(value = "signTime", required = false) String signTime) {

        return homeService.myBill(salesmanId,type,signTime);
    }


    /**
     * 提现申请
     */
    @PostMapping("/withdraw")
    @ApiOperation("提现申请")
    public R<Boolean> withdraw(@RequestParam(value = "salesmanId", required = true) Long salesmanId,
                               @RequestParam(value = "withdrawAmount", required = true) Integer withdrawAmount) {
        return R.ok(homeService.withdraw(salesmanId, withdrawAmount));
    }

    /**
     * 提现记录
     */
    @PostMapping("/withdrawDepositRecord")
    @ApiOperation("提现记录")
    public AjaxResult withdrawDepositRecord(@RequestParam("salesmanId") Long salesmanId) {

        return homeService.withdrawDepositRecord(salesmanId);
    }

    /**
     * 个人信息
     */
    @PostMapping("/personalInformation")
    @ApiOperation("个人信息")
    public AjaxResult personalInformation(@RequestParam("salesmanId") Long salesmanId) {

        return homeService.personalInformation(salesmanId);
    }

}
